package test.hxy.mongoSchoolSimple.dao;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

import static org.springframework.data.mongodb.core.aggregation.Aggregation.group;
import static org.springframework.data.mongodb.core.aggregation.Aggregation.match;

/**
 * @Author: Kevin
 * @Date: 2021/6/14 20:16
 */
@Repository
public class CountDao {
    @Autowired
    private MongoTemplate mongoTemplate;

    public List<Map> countTeachersGroupByCampus() {
        Aggregation aggregation = Aggregation.newAggregation(
                Aggregation.group("campusId").count().as("teacherNumber")
        );
        List<Map> maps = mongoTemplate.aggregate(aggregation, "teacher", Map.class).getMappedResults();
        maps.forEach(map -> map.put("campusId", map.remove("_id")));
        return maps;
    }

//    public List<Map> countBooksGroupByCampus() {
//        Aggregation aggregation = Aggregation.newAggregation(
//                Aggregation.group("campusId").count().as("bookNumber")
//        );
//        List<Map> maps = mongoTemplate.aggregate(aggregation, "book", Map.class).getMappedResults();
//        maps.forEach(map -> map.put("campusId", map.remove("_id")));
//        return maps;
//    }
}
